package com.qiantu.phone.ui.activity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.base.BaseActivity;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLDeviceActionDBManager;
import com.qiantu.api.db.LLDeviceCustomDBManager;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBaseConfigInfo;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceBindingData;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.DeviceState;
import com.qiantu.api.entity.KGSwitchBindBean;
import com.qiantu.api.entity.QueryCodeResultBean;
import com.qiantu.api.entity.ScanWirelessControllerBean;
import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.api.entity.SwitchBindActionBean;
import com.qiantu.api.entity.SwitchBindBean;
import com.qiantu.api.entity.UniversalDeviceData;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.bean.KGUISwitchBindBean;
import com.qiantu.phone.bean.UISwitchBindBean;
import com.qiantu.phone.ui.adapter.KGSwitchBindAdapter;
import com.qiantu.phone.ui.adapter.SwitchBindAdapter;
import com.qiantu.phone.ui.dialog.ConfirmCancelHintDialog;
import com.qiantu.phone.ui.dialog.ConfirmHintDialog;
import com.qiantu.phone.ui.dialog.WaitFinishDialog;
import com.qiantu.phone.ui.fragment.SceneFragment;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.utils.SceneDetailUtil;
import com.qiantu.phone.utils.TempDataHolder;
import com.qiantu.phone.widget.OperateBindView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class KGPanelBindInfoActivity  extends AppActivity {
    private RecyclerView recyclerView;
    private KGSwitchBindAdapter switchBindAdapter;

    private List<KGSwitchBindBean> switchBindBeans;

    public static void start(Context context, String controllerSerialNo) {

        Intent intent = new Intent(context, KGPanelBindInfoActivity.class);
        intent.putExtra("controllerSerialNo", controllerSerialNo);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_switch_bind;
    }

    @Override
    protected void initView() {
        getTitleBar().setTitle(R.string.smart_panel_bind_info);
        findViewById(R.id.empty_group).setVisibility(View.GONE);
        recyclerView = findViewById(R.id.recycler_view);
        setOnClickListener(R.id.add_switch_binding);
    }

    private String controllerSerialNo;//绑定的设备唯一码
    @Override
    protected void initData() {
        controllerSerialNo = getString("controllerSerialNo");
        LLHttpManager.getKGPanelList(this, controllerSerialNo, new HttpCallback<HttpData<List<KGSwitchBindBean>>>(this) {
            @Override
            public void onSucceed(HttpData<List<KGSwitchBindBean>> result) {
                super.onSucceed(result);
                switchBindBeans = result.getData();
                buildUIData(switchBindBeans);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }
        });
    }

    private void buildUIData(List<KGSwitchBindBean> switchBindBeans) {
        List<KGUISwitchBindBean> uiSwitchBindBeans = new ArrayList<>();

            LinkedHashMap<String, ArrayList<KGSwitchBindBean>> uiSwitchBindMap = new LinkedHashMap<>();
            for (KGSwitchBindBean bean : switchBindBeans){
                String title = bean.getActionType() + " " + bean.getBindActionType() + " " + bean.getBindActionName();

                if (!TextUtils.isEmpty(title)){
                    ArrayList<KGSwitchBindBean> list = uiSwitchBindMap.get(title);
                    if (list == null){
                        list = new ArrayList<>();
                        list.add(bean);
                        uiSwitchBindMap.put(title, list);
                    }else {
                        list.add(bean);
                    }
                }
            }
            for (String title : uiSwitchBindMap.keySet()){
                KGUISwitchBindBean uiSwitchBindBean = new KGUISwitchBindBean();
                uiSwitchBindBean.setBindActionName(title);
                uiSwitchBindBean.setSwitchBindBeans(uiSwitchBindMap.get(title));
                uiSwitchBindBeans.add(uiSwitchBindBean);
            }

        switchBindAdapter = new KGSwitchBindAdapter(KGPanelBindInfoActivity.this);
        switchBindAdapter.setData(uiSwitchBindBeans);
        recyclerView.setAdapter(switchBindAdapter);
        switchBindAdapter.setOnChildClickListener(R.id.tv_device_name,new BaseAdapter.OnChildClickListener(){
            @Override
            public void onChildClick(RecyclerView recyclerView, View childView, int position) {
                KGUISwitchBindBean item = uiSwitchBindBeans.get(position);
                KGSwitchBindBean bean =  item.getSwitchBindBeans().get(0);
                if(bean.getBindType() == 1 || bean.getBindType() == 3){
                    DeviceSettingActivity.start(getContext(), bean.getBindSerialNo(), true);
                }else{
                    SceneDetailUtil.getSceneDetailAndToEdit(KGPanelBindInfoActivity.this, KGPanelBindInfoActivity.this, bean.getBindSerialNo());
                }
            }
        });

    }
}
